const typeService = require('../services/typeService')

const typeController = {
  async getType(req, res, next) {
    try {
      let data = await typeService.getType()
      res.send({
        code: 200,
        msg: 'ok',
        data,
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: error.message,
      })
    }
  },
  async getTypeByName(req, res, next) {
    try {
      let data = await typeService.getTypeByName(req.query.q)
      res.send({
        code: 200,
        msg: 'ok',
        data,
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: error.message,
      })
    }
  },
  async getTypeList(req, res, next) {
    try {
      let data = await typeService.getTypeList(req.params.id)
      res.send({
        code: 200,
        msg: 'ok',
        data,
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: error.message,
      })
    }
  },
  async getTypeInfo(req, res, next) {
    try {
      let data = await typeService.getTypeInfo(req.params.id)
      res.send({
        code: 200,
        msg: 'ok',
        data,
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: error.message,
      })
    }
  },
  async addType(req, res, next) {
    try {
      let data = await typeService.addType(req.body)
      res.send({
        code: 200,
        msg: 'ok',
        data,
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: error.message,
      })
    }
  },
  async updateType(req, res, next) {
    try {
      let body = req.body
      let id = body._id
      delete body.id
      let data = await typeService.updateType(id, body)
      res.send({
        code: 200,
        msg: 'ok',
        data,
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: error.message,
      })
    }
  },
  async deleteType(req, res, next) {
    try {
      let data = await typeService.deleteType(req.body.id)
      res.send({
        code: 200,
        msg: 'ok',
        data,
      })
    } catch (error) {
      res.send({
        code: 400,
        msg: error.message,
      })
    }
  },
}

module.exports = typeController
